﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.zoo.ui {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import Classes.*;
    import __AS3__.vec.*;
    import Modules.zoo.events.*;

    public class EnclosureBackdrop extends Sprite {

        private static const SPEECH_BUBBLE_HIDE_TIME:int = 500;

        private var m_enclosureDefinition:Item;
        private var m_enclosureSlots:Vector.<ZooEnclosureSlot>;
        private var m_background:DisplayObject;
        private var m_speechBubble:ZooSpeechBubble;
        private var m_speechBubbleHideTimeout:uint;

        public function EnclosureBackdrop(_arg1:String){
            addEventListener(Event.REMOVED_FROM_STAGE, this.onRemoveFromStage, false, 0, true);
            this.m_enclosureDefinition = Global.gameSettings.getItemByName(_arg1);
            this.m_enclosureSlots = new Vector.<ZooEnclosureSlot>();
            this.loadBackground();
            var _local2 = (Global.world.getObjectsByNames([_arg1]).length > 0);
            if (_local2){
                this.prepareEnclosureSlots();
            };
            this.prepareSpeechBubble();
        }
        public function get background():DisplayObject{
            return (this.m_background);
        }
        public function get speechBubble():ZooSpeechBubble{
            return (this.m_speechBubble);
        }
        private function loadBackground():void{
            var _local1:String = this.m_enclosureDefinition.getImageByName("enclosure");
            if (_local1){
                LoadingManager.loadFromUrl(_local1, {
                    priority:LoadingManager.PRIORITY_LOW,
                    completeCallback:this.onLoadBackgroundComplete
                });
            };
        }
        private function setBackground(_arg1:DisplayObject):void{
            if (((this.m_background) && (this.m_background.parent))){
                this.m_background.parent.removeChild(this.m_background);
            };
            this.m_background = _arg1;
            dispatchEvent(new ZooDialogEvent(ZooDialogEvent.DISPLAY_UPDATE));
        }
        private function prepareEnclosureSlots():void{
            var _local3:Point;
            var _local4:ZooEnclosureSlot;
            var _local1:Array = this.m_enclosureDefinition.displayLocations.dialogDisplayLocations;
            var _local2:int;
            while (_local2 < _local1.length) {
                _local3 = (_local1[_local2] as Point);
                _local4 = new ZooEnclosureSlot();
                _local4.index = _local2;
                _local4.x = (_local3.x - (_local4.width * 0.5));
                _local4.y = (_local3.y - _local4.height);
                _local4.addEventListener(Event.CLOSE, this.onEnclosureSlotClose, false, 0, true);
                _local4.addEventListener(MouseEvent.ROLL_OVER, this.onEnclosureSlotRollOver, false, 0, true);
                _local4.addEventListener(MouseEvent.ROLL_OUT, this.onEnclosureSlotRollOut, false, 0, true);
                addChild(_local4);
                this.m_enclosureSlots.push(_local4);
                _local2++;
            };
            dispatchEvent(new ZooDialogEvent(ZooDialogEvent.DISPLAY_UPDATE));
        }
        private function prepareSpeechBubble():void{
            this.m_speechBubble = new ZooSpeechBubble();
            this.m_speechBubble.x = 30;
            this.m_speechBubble.y = 20;
            addChild(this.m_speechBubble);
            this.m_speechBubble.addEventListener(Event.CLOSE, this.onSpeechBubbleClose, false, 0, true);
        }
        private function hideSpeechBubble():void{
            if (((this.m_speechBubble) && (this.m_speechBubble.parent))){
                this.m_speechBubble.parent.removeChild(this.m_speechBubble);
            };
        }
        public function addAnimalToSlot(_arg1:String, _arg2:int):void{
            if (this.m_enclosureSlots.length > _arg2){
                this.m_enclosureSlots[_arg2].addAnimal(_arg1);
            };
        }
        private function onLoadBackgroundComplete(_arg1:Event):void{
            var _local2:LoaderInfo = (_arg1.currentTarget as LoaderInfo);
            this.setBackground(_local2.content);
        }
        private function onEnclosureSlotClose(_arg1:Event):void{
            var _local3:ZooDialogEvent;
            var _local2:ZooEnclosureSlot = (_arg1.currentTarget as ZooEnclosureSlot);
            if (_local2.isOccupied){
                _local3 = new ZooDialogEvent(ZooDialogEvent.REMOVE_ANIMAL_FROM_DISPLAY);
                _local3.animalItemName = _local2.animalDefinition.name;
                _local3.slotIndex = _local2.index;
                dispatchEvent(_local3);
                _local2.removeAnimal();
            };
        }
        private function onEnclosureSlotRollOver(_arg1:MouseEvent):void{
            if ((_arg1.currentTarget as ZooEnclosureSlot).isOccupied){
                clearTimeout(this.m_speechBubbleHideTimeout);
                this.m_speechBubbleHideTimeout = setTimeout(this.hideSpeechBubble, SPEECH_BUBBLE_HIDE_TIME);
            };
        }
        private function onEnclosureSlotRollOut(_arg1:MouseEvent):void{
            clearTimeout(this.m_speechBubbleHideTimeout);
            addChild(this.m_speechBubble);
        }
        private function onSpeechBubbleClose(_arg1:Event):void{
            dispatchEvent(new Event(Event.CLOSE));
        }
        private function onRemoveFromStage(_arg1:Event):void{
            clearTimeout(this.m_speechBubbleHideTimeout);
        }

    }
}//package Modules.zoo.ui 
